package com.deer.wms.base.system.webservice.takegoodsbill;

import cn.hutool.core.date.DateUtil;
import com.deer.wms.base.system.dao.bill.BillInSapDetailMapper;
import com.deer.wms.base.system.dao.bill.BillInSapMapper;
import com.deer.wms.base.system.model.bill.BillInSap;
import com.deer.wms.base.system.model.bill.BillInSapDetail;
import com.deer.wms.base.system.service.bill.BillInSapDetailService;
import com.deer.wms.base.system.web.billsend.MM101Send;
import com.deer.wms.base.system.web.billsend.MM101_ZDATA_MSEG;
import com.deer.wms.base.system.webservice.model.saprequest.ConstantTrans;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import wms.tjsemi.com.DT_WMS_TO_ERP_REQ;
import wms.tjsemi.com.DT_WMS_TO_ERP_RESP;
import wms.tjsemi.com.SI_WMS_TO_ERP_OUT_SYN;
import wms.tjsemi.com.SI_WMS_TO_ERP_OUT_SYNServiceLocator;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 申紫微
 * @version 1.0
 * @description
 * @data 2021-07-24 16:25:18
 */
@Controller
@Slf4j
public class TaskGoodsBillController {

    @Autowired
    BillInSapMapper billInSapMapper;
    @Autowired
    private BillInSapDetailMapper billInSapDetailMapper;

    @Autowired
    BillInSapDetailService billInSapDetailService;


    @GetMapping("/sendMM101")
    public void sendMM101ToSap() throws Exception{
        try {
            BillInSap billInSap = new BillInSap();
            billInSap.setState(1);
            List<BillInSap> select = billInSapMapper.select(billInSap);
            for (BillInSap b : select) {
                List<MM101Send> zdataList = new ArrayList<>();
                String now = DateUtil.now();
                BillInSapDetail billInSapDetail = new BillInSapDetail();
                //billInSapDetail.setVBELN(b.getVBELN());
                List<BillInSapDetail> select1 = billInSapDetailMapper.select(billInSapDetail);
                MM101Send mm101Send = new MM101Send();
//                MM101_ZDATA_MKPF mkpf = new MM101_ZDATA_MKPF();
                mm101Send
                        //.setWMSHN(b.getVBELN()) // WMS单号
                    .setBLDAT(now)// 凭证日期
                    .setBUDAT(now)// 过账日期
                    .setBWART(ConstantTrans.EL);// 移动类型
                List<MM101_ZDATA_MSEG> list = new ArrayList();
                for (BillInSapDetail bs : select1) {
                    MM101_ZDATA_MSEG mesg = new MM101_ZDATA_MSEG();
                    // todo 下面4行需要进一步明确
//                    mesg.setWMSHN(bs.getVBELN());
//                    mesg.setWMSIN(bs.getPOSNR());
//                    mesg.setVBELN_IM(bs.getVBELN());
//                    mesg.setVBELP_IM(bs.getPOSNR());
//                    mesg.setWERKS(bs.getWERKS());
//                    mesg.setLGORT(bs.getLGORT());
//                    mesg.setERFMG(String.valueOf(bs.getLGMNG()));
//                    mesg.setERFME(bs.getMEINS());
//                    mesg.setCHARG(bs.getCHARG());
                    list.add(mesg);
                }
                mm101Send.setITEM(list);
                zdataList.add(mm101Send);
                Gson gson = new Gson();
                String zdataJsonString = gson.toJson(zdataList);
                SI_WMS_TO_ERP_OUT_SYN http_port = new SI_WMS_TO_ERP_OUT_SYNServiceLocator().getHTTP_Port();
                DT_WMS_TO_ERP_REQ req = new DT_WMS_TO_ERP_REQ();
                req.setSYSID("W1041KW");
                req.setIFYWID("MM101");
                req.setZDATA(zdataJsonString);
                req.setBSKEY("34567");
                req.setSAPKEY("ddddd");
                log.info("WMS发送给SAP的数据:{}" + req.toString());
                DT_WMS_TO_ERP_RESP resp = http_port.SI_WMS_TO_ERP_OUT_SYN(req);
                log.info("SAP返回结果:{}" + resp.toString());

                //返回值为正常时更新数据状态为已过帐
                if (resp.getZTYPE().equals("正常")) {
                    BillInSapDetail bd = new BillInSapDetail();
                    //billInSapDetail.setVBELN(b.getVBELN());
                    billInSapDetailService.updateCarryState(billInSapDetail);
                    BillInSap bp = new BillInSap();
                    //bp.setVBELN(b.getVBELN());
                    billInSapMapper.updatestate(billInSap);

                }
            }

        } catch (Exception e){
            log.error("MM101过账给SAP失败,错误信息:{}",e.getMessage());
        }
    }

}
